﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ConstantCMapEditor3
{
    public class PositionChecker
    {
        const int X_NUM = 2000;
        const int Y_NUM = 2000;
        const int UNIT = 32;

        int [,] PositionArray = new int[X_NUM,Y_NUM];

        public void Init() 
        {
            for (int i = 0; i < X_NUM; i++) 
            {
                for (int j = 0; j < Y_NUM; j++) 
                {
                    PositionArray[i,j]=0;
                }
            }
        }

        public PositionInformation SpritePositionTranslation(Vector2 Position,Vector2 WidthHeight,float Rotate)
        {
            Vector2 ProxyPosition = Position;

            PositionInformation info = new PositionInformation();

            int difX = 0;
            int difY = 0;


            if (Math.Abs(Math.Cos(Rotate)) > 0.5)
            {
                if (((int)WidthHeight.X) % 64 == 0) difX = 16;
                if (((int)WidthHeight.Y) % 64 == 0) difY = 16;
            }

            if (Math.Abs(Math.Cos(Rotate)) < 0.5)
            {
                if (((int)WidthHeight.X) % 64 == 0) difY = 16;
                if (((int)WidthHeight.Y) % 64 == 0) difX = 16;
            }



            int X;
            if (ProxyPosition.X >= 1)
                X = (int)ProxyPosition.X - ((int)ProxyPosition.X) % 32 + 1;
            else
                X = (int)ProxyPosition.X - ((int)ProxyPosition.X) % 32 + 1 - 32;


            int Y;
            if (ProxyPosition.Y >= 1)
                Y = (int)ProxyPosition.Y - ((int)ProxyPosition.Y) % 32 + 1;
            else
                Y = (int)ProxyPosition.Y - ((int)ProxyPosition.Y) % 32 + 1 - 32;

            info.Position = new Vector2(X + difX, Y + difY);
            info.PositionHolder = new List<Point>();
            
            Vector2 LeftTop = info.Position - WidthHeight/2;
            LeftTop /= 32;

            for(int i=0;i<WidthHeight.X/32;i+=1)
            {
                for (int j = 0; j < WidthHeight.Y / 32; j += 1)
                {
                    Vector2 PositionPoint = new Vector2(LeftTop.X+i,LeftTop.Y+j);
                    PositionPoint += new Vector2(X_NUM/2,X_NUM/2);
                    info.PositionHolder.Add(new Point((int)PositionPoint.X,(int)PositionPoint.Y));
                }
            }

            return info;
        }

        public void RegisterPosition(List<Point> list)
        {
            if (list == null) return;

            foreach (Point p in list) 
            {
                PositionArray[p.X, p.Y]++;
            }
        }

        public void UnRegisterPosition(List<Point> list) 
        {
            if (list == null) return;

            foreach (Point p in list)
            {
                PositionArray[p.X, p.Y]--;
            }
        }

        public bool IsCollide(List<Point> list)
        {
            if (list == null) return false;

            bool result = false;


            foreach (Point p in list)
            {
                if (PositionArray[p.X, p.Y] != 0) result = true;
            }

            //if (result) throw new Exception();

            return result;
        }
    }

    public class PositionInformation
    {
        public Vector2 Position;
        public List<Point> PositionHolder;
    }
}
